package com.ruoyi.service.impl;

import java.util.Date;
import java.util.List;


import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.domain.Sendnumber;
import com.ruoyi.domain.T03;
import com.ruoyi.domain.T06;
import com.ruoyi.domain.T18;
import com.ruoyi.framework.web.service.SysRegisterService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.mapper.T07Mapper;
import com.ruoyi.service.*;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.impl.SysRoleServiceImpl;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import com.ruoyi.uitity.MinioUtil;
import com.ruoyi.uitity.WxMessageProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.T18Mapper;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.xml.crypto.Data;

/**
 * 客户管理员Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-31
 */
@Service
public class T18ServiceImpl implements IT18Service
{
   @Autowired
   private T18Mapper t18Mapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IT06Service t06Service;
    @Autowired
    private IT07Service t07Service;
    @Autowired
    private WxMessageProducer producer;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private SysRegisterService registerService;
    @Autowired
    private SysRoleServiceImpl sysRoleService;

    @Autowired
    private IT03Service t03Service;
    @Autowired
    private IT05Service t05Service;
    @Autowired
    private SysUserServiceImpl sysUserServiceImpl;

    /**
     * 查询客户管理员
     *
     * @param F1801 客户管理员主键
     * @return 客户管理员
     */
    @Override
    public T18 selectT18ByF1801(Long F1801)
    {
        return t18Mapper.selectT18ByF1801(F1801);
    }

    /**
     * 查询客户管理员列表
     *
     * @param t18 客户管理员
     * @return 客户管理员
     */
    @Override
    public List<T18> selectT18List(T18 t18)
    {
        PageUtils.startPage();
        return t18Mapper.selectT18List(t18);
    }

    /**
     * 新增客户管理员
     *
     * @param t18 客户管理员
     * @return 结果
     */
    @Override
    @Transactional
    public int insertT18(T18 t18)
    {
        return t18Mapper.insertT18(t18);
    }

    /**
     * 修改客户管理员
     *
     * @param t18 客户管理员
     * @return 结果
     */
    @Override
    public int updateT18(T18 t18)
    {
        return t18Mapper.updateT18(t18);
    }

    /**
     * 批量删除客户管理员
     *
     * @param F1801s 需要删除的客户管理员主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteT18ByF1801s(Long[] F1801s)
    {
        sysUserServiceImpl.deleteUserByIds(F1801s);
        return t18Mapper.deleteT18ByF1801s(F1801s);
    }

    /**
     * 删除客户管理员信息
     *
     * @param F1801 客户管理员主键
     * @return 结果
     */
    @Override
    public int deleteT18ByF1801(Long F1801)
    {
        return t18Mapper.deleteT18ByF1801(F1801);
    }

    @Override
    public List<T18> selectT18ListByF1801s(List<Long> F1801s) {
        return t18Mapper.selectT18ListByF1801s(F1801s);
    }

    public Long selectF0301ByF1801(Long userId) {
        return t18Mapper.selectF0301ByF1801(userId);
    }

    @Override
    public List<T18> selectT18ByComIdList(T18 t18, Long comId) {
        return t18Mapper.selectT18ByComIdList(t18,comId);
    }


    @Autowired
    private IT18Service t18Service;
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    @Override
    public String addUser(RegisterBody registerBody,Long ComId,T18 yuanT18) {
        String msg=registerService.register(registerBody);
        SysUser user =userService.selectUserByUserName(registerBody.getUsername());
        Long userId = user.getUserId();
        user.setUserName(registerBody.getUsername());
        userService.updateUser(user);
        if(!t18Service.checkUserNameUnique(registerBody.getUsername())){
            return  "用户名重复";
        }
        T18 t18 = new T18();
        t18.setF1802(SecurityUtils.encryptPassword(registerBody.getPassword()));
        t18.setF1804(registerBody.getName());
        t18.setF1806(registerBody.getUsername());
        t18.setF0301(ComId);
        t18.setF1807(0L);
        t18.setF1801(userId);
        Date now = new Date();
        t18.setF1809(0L);
        t18.setF1803(now);
        T03 t03= t03Service.selectT03ByF0301(ComId);
        t18.setF1808(t03.getF0302());
        t18.setF1809(yuanT18.getF1809());
        if(!ObjectUtils.isEmpty(yuanT18.getF0501())){
            t18.setF1810(t05Service.selectT05ByF0501(yuanT18.getF0501()).getF0502());
            t18.setF0501(yuanT18.getF0501());
        }

        try {
            t18Service.insertT18(t18);
        } catch (Exception e) {
            throw new RuntimeException("插入T18表失败", e);
        }
        Long  lon[] = new Long[1];
        lon[0] = userId;
        //插入角色


        sysRoleService.insertAuthUsers(100L,lon);

        if(msg.isEmpty()){
            return "";
        }
        return msg;
    }

    @Override
    public boolean checkUserNameUnique(String username) {
        T18  t18=t18Mapper.checkUserNameUnique(username);
        if (t18!=null){
            return false;
        }
        return true;
    }
}
